<?php

require_once('../../includes/classes/core.php');
$action 	= $_REQUEST['act'];
$error		= '';
$data		= '';


$start		= $_REQUEST['start'];
$end		= $_REQUEST['end'];


$start = $db->convertDateTime($start);
$end = $db->convertDateTime($end);

switch ($action) {
	case 'get_list' :
		$count = 		$_REQUEST['count'];
		$hidden = 		$_REQUEST['hidden'];
		
	  	$rResult = mysql_query("SELECT DATE_FORMAT(asterisk_incomming.call_datetime,'%d/%m/%y %H:%i:%s'),
									   DATE_FORMAT(asterisk_incomming.call_datetime,'%d/%m/%y %H:%i:%s'),
								       asterisk_incomming.source,
								       asterisk_incomming.dst_extension,
											crystal_users.`name`,
								       TIME_FORMAT(SEC_TO_TIME(asterisk_incomming.duration ),'%i:%s') as `time`,
								       CONCAT('<p onclick=play(', '\'', asterisk_incomming.file_name, '\'',  ')>მოსმენა</p>', '<a download=\"image.jpg\" href=\"http://91.233.15.136:8181/', asterisk_incomming.file_name, '\">ჩამოტვირთვა</a>')
								FROM   asterisk_incomming
								JOIN   crystal_users ON asterisk_incomming.user_id=crystal_users.id
								WHERE    `asterisk_incomming`.`call_datetime`  BETWEEN  '$start'  And '$end' 
								AND asterisk_incomming.dst_extension IN (501,502,503,504,505)
								UNION ALL
								SELECT DATE_FORMAT(asterisk_outgoing.call_datetime,'%d/%m/%y %H:%i:%s'),
								DATE_FORMAT(asterisk_outgoing.call_datetime,'%d/%m/%y %H:%i:%s'),
	  										asterisk_outgoing.extension,
											asterisk_outgoing.phone,
											crystal_users.`name`,
						       	TIME_FORMAT(SEC_TO_TIME(asterisk_outgoing.duration ),'%i:%s') as `time`,
						       	CONCAT('<p onclick=play(', '\'', asterisk_outgoing.file_name, '\'',  ')>მოსმენა</p>', '<a download=\"image.jpg\" href=\"http://91.233.15.136:8181/', asterisk_outgoing.file_name, '\">ჩამოტვირთვა</a>')
								FROM   asterisk_outgoing
								JOIN   crystal_users ON asterisk_outgoing.user_id=crystal_users.id
								WHERE    `asterisk_outgoing`.`call_datetime`  BETWEEN  '$start'  And '$end' 
	  							AND asterisk_outgoing.extension IN (501,502,503,504,505)");
	  	
		$data = array(
				"aaData"	=> array()
		);

		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				/* General output */
				$row[] = $aRow[$i];
			}
			$data['aaData'][] = $row;
		}

		break;
		
	default:
		$error = 'Action is Null';
}

$data['error'] = $error;

echo json_encode($data);

?>